package com.gitee.mars.studyabroad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.studyabroad.constant.Constant;
import com.gitee.mars.studyabroad.entity.AbroadSchool;
import com.gitee.mars.studyabroad.mapper.AbroadSchoolMapper;
import com.gitee.mars.studyabroad.request.UserCollectAbroadSchoolRequest;
import com.gitee.mars.studyabroad.response.UserCollectAbroadSchoolResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.studyabroad.service.IUserCollectAbroadSchoolService;
import com.gitee.mars.studyabroad.mapper.UserCollectAbroadSchoolMapper;
import com.gitee.mars.studyabroad.entity.UserCollectAbroadSchool;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * UserCollectAbroadSchool 实现层
 *
 * @Author wq
 * @Date 2021-04-22
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserCollectAbroadSchoolServiceImpl extends ServiceImpl<UserCollectAbroadSchoolMapper, UserCollectAbroadSchool> implements IUserCollectAbroadSchoolService {


    @Resource
    private AbroadSchoolMapper abroadSchoolMapper;

    @Override
    public void addOrCancel(UserCollectAbroadSchoolRequest request) {
        UserCollectAbroadSchool selectOne = baseMapper.selectOne(new LambdaQueryWrapper<UserCollectAbroadSchool>().
                eq(UserCollectAbroadSchool::getAbroadSchoolId, request.getSchoolId()).
                eq(UserCollectAbroadSchool::getUserId, request.getUserId()));
        if (Objects.isNull(selectOne)) {
            //收藏
            UserCollectAbroadSchool userCollectAbroadSchool = new UserCollectAbroadSchool();
            userCollectAbroadSchool.setAbroadSchoolId(request.getSchoolId());
            userCollectAbroadSchool.setUserId(request.getUserId());
            userCollectAbroadSchool.setStatus(Constant.ONE);
            userCollectAbroadSchool.setCreateTime(new Date());
            userCollectAbroadSchool.setUpdateTime(new Date());
            save(userCollectAbroadSchool);
        } else {
            if (selectOne.getStatus() == Constant.ONE) {
                if (request.getStatus() == Constant.TWO) {
                    //取消收藏
                    selectOne.setStatus(request.getStatus());
                    selectOne.setUpdateTime(new Date());
                    updateById(selectOne);
                }
            } else {
                selectOne.setStatus(request.getStatus());
                selectOne.setUpdateTime(new Date());
                updateById(selectOne);
            }
        }
    }

    @Override
    public List<UserCollectAbroadSchoolResponse> collectList(Integer userId) {
        ArrayList<UserCollectAbroadSchoolResponse> list = new ArrayList<>();
        List<UserCollectAbroadSchool> userCollectCountryHomes = baseMapper.selectList(new LambdaQueryWrapper<UserCollectAbroadSchool>().
                eq(UserCollectAbroadSchool::getUserId, userId).eq(UserCollectAbroadSchool::getStatus, Constant.ONE));
        for (UserCollectAbroadSchool userCollectAbroadSchool : userCollectCountryHomes) {
            UserCollectAbroadSchoolResponse response = new UserCollectAbroadSchoolResponse();
            Integer userId1 = userCollectAbroadSchool.getUserId();
            response.setUserId(userId1);
            Integer schoolId = userCollectAbroadSchool.getAbroadSchoolId();
            AbroadSchool school = abroadSchoolMapper.selectOne(new LambdaQueryWrapper<AbroadSchool>().
                    eq(AbroadSchool::getId, schoolId));
            if (Objects.nonNull(school)) {
                response.setCity(school.getCity());
                response.setSchoolId(school.getId());
                response.setAddress(school.getAddress());
                response.setIntroduction(school.getIntroduction());
                response.setName(school.getName());
                response.setImage(school.getImage());
                response.setType(school.getType());
                response.setCreateTime(school.getCreateTime());
                list.add(response);
            }
        }
        return list;
    }
}